package com.finace.money.data;

import com.finace.money.common.Constant;
import com.finace.money.common.util.ParamUtil;
import com.finace.money.data.bean.BaseBean;
import com.finace.money.data.bean.EmptyBean;
import com.finace.money.data.http.ApiService;
import com.finace.money.presenter.contract.UploadImageContract;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.TreeMap;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by zhangpeng on 2018/2/1.
 */

public class UploadImageModel implements UploadImageContract.IUploadImageModel {

    private ApiService mApiService;

    public UploadImageModel(ApiService mApiService) {
        this.mApiService = mApiService;
    }

    @Override
    public Observable<BaseBean<EmptyBean>> register(String phone, String imgcode, String code, String name,
                                                    String email, String cardno, String province, String city,
                                                    String county, String specific_address, File face_card, File identity_card) {

        TreeMap<String, String> map = new TreeMap<>();
        map.put("phone", phone);
        map.put("imgcode",imgcode);
        map.put("code", code);
        map.put("cardno", cardno);


        String encodeName = null;
        String encodeProvince = null;
        String encodeCity = null;
        String encodeCounty = null;
        String encodeSpe = null;
        String encodeEmail = null;
        try {
            encodeName = URLEncoder.encode(name.trim(), "UTF-8");
            encodeProvince = URLEncoder.encode(province.trim(), "UTF-8");
            encodeCity = URLEncoder.encode(city.trim(), "UTF-8");
            encodeCounty = URLEncoder.encode(county.trim(), "UTF-8");
            encodeSpe = URLEncoder.encode(specific_address.trim(), "UTF-8");
            encodeEmail = URLEncoder.encode(email.trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        map.put("name", encodeName);
        map.put("province", encodeProvince);
        map.put("city", encodeCity);
        map.put("county", encodeCounty);
        map.put("specific_address", encodeSpe);
        map.put("email", encodeEmail);

//        String path1 = Environment.getExternalStorageDirectory() + File.separator + "xx.png";
//        String path2 = Environment.getExternalStorageDirectory() + File.separator + "aa.png";
//        File file = new File(path1);
//        File file1 = new File(path2);
        Map<String, String> parma = ParamUtil.getParma(map);
        map.put("name", name);
        map.put("province", province);
        map.put("city", city);
        map.put("county", county);
        map.put("specific_address", specific_address);
        map.put("email", email);

        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("face_card", face_card.getName(), RequestBody.create(MediaType.parse("image/jpg"), face_card))
                .addFormDataPart("identity_card", identity_card.getName(), RequestBody.create(MediaType.parse("image/jpg"), identity_card));
        for (Map.Entry<String, String> entry : parma.entrySet()) {
            builder.addFormDataPart(entry.getKey(), entry.getValue());
        }

        return mApiService.register(Constant.registerInterface, builder.build());
    }

}
